package com.example.zhu.service.Impl;

import com.example.zhu.entity.Activity;
import com.example.zhu.entity.ActivityRecord;
import com.example.zhu.mapper.ActivityMapper;
import com.example.zhu.mapper.ActivityRecordMapper;
import com.example.zhu.mapper.AddressBookMapper;
import com.example.zhu.mapper.UsersMapper;
import com.example.zhu.result.Result;
import com.example.zhu.service.ActivityService;
import com.example.zhu.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityRecordMapper activityRecordMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 查询活动列表
     *
     * @return
     */
    public List<Activity> selectAll() {
        List<Activity> list = activityMapper.selectAll();
        return list;
    }

    /**
     * 查询活动信息
     *
     * @param id
     * @return
     */
    public Activity getEditInfo(Integer id) {
        Activity activity = activityMapper.getById(id);
        return activity;
    }

    /**
     * 激活活动，向活动记录表中添加老用户未参加的活动
     * @param area
     */
    @Transactional
    public void activate(List<String> area, Integer id) {
//        List<String> openids = usersMapper.selectAllOpenid();
        List<UserVo> users = addressBookMapper.selectAllUsers();
        List<ActivityRecord> activityRecords = activityRecordMapper.selectAll();
        for (String a : area) {
            for (UserVo user : users) {
                String openid = user.getOpenid();
                String addr = user.getAddress();
                String[] split = addr.split("/");
                String address = split[0];
                if(address.equals(a)){
                    int i = 0;
                    for (; i < activityRecords.size(); i++) {
                        ActivityRecord activityRecord = activityRecords.get(i);
                        String openid_ = activityRecord.getOpenid();
                        Integer activityId_ = activityRecord.getActivityId();
                        if (openid.equals(openid_) && activityId_ == id) {
                            break;
                        }
                    }
                    if (i >= activityRecords.size()) {
                        ActivityRecord activityRecord = ActivityRecord.builder().activityId(id)
                                .openid(openid)
                                .recordStartTime(LocalDateTime.now())
                                .RecordFinished(2)
                                .build();
                        activityRecordMapper.insert(activityRecord);
                    }
                }

            }
        }

    }

    /**
     * 编辑活动
     *
     * @param activity
     * @return
     */
    public Result update(Activity activity) {
        Integer activityId = activity.getActivityId();
        Activity activity_ = activityMapper.selectById(activityId);
        Integer activityStatus = activity_.getActivityStatus();
        if (activityStatus == 0 || activityStatus == -1) {
            return Result.error("活动未启用或已结束");
        }
        activityMapper.update(activity);
        return Result.success();
    }
}
